package org.viettan.android.main.activities;

import org.viettan.android.main.R;
import org.viettan.android.main.activities.articles.NewsActivity;
import org.viettan.android.main.activities.articles.SectionListActivity;
import org.viettan.android.main.activities.campaign.CampaignListActivity;
import org.viettan.android.main.activities.common.VietTanBaseTabActivity;
import org.viettan.android.main.activities.info.InfoActivity;
import org.viettan.android.main.activities.radio.RadioActivity;
import org.viettan.android.main.managers.SettingsManager;
import org.viettan.android.main.model.Language;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;

import com.google.inject.Inject;

/**
 * The root activity that displays all main tabs and allows you to switch between them.
 */
public class RootActivity extends VietTanBaseTabActivity implements OnTabChangeListener {
    // must match with style.xml
    private final int TAB_BACKGROUND_COLOR_SELECTED = Color.parseColor("#FFFFFF");
    private final int TAB_BACKGROUND_COLOR_UNSELECTED = Color.parseColor("#DDDDDD");
    private final int[] tabTitles = new int[] { R.string.menu_news, R.string.menu_sections,
	    R.string.menu_radio, R.string.menu_action, R.string.menu_info };

    private TabHost tabHost;
    private Resources res;
    private Language lastLanguage;

    @Inject
    SettingsManager settingsManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.root);
	tabHost = getTabHost();

	// adding the five tabs
	addTabs();

	// force first tab to be selected
	tabHost.setCurrentTab(0);
	onTabChanged(null);
    }

    private void addTabs() {
	res = getResources();
	lastLanguage = settingsManager.getArticleLanguage();
	addTab("news", NewsActivity.class, R.string.menu_news, R.drawable.latest);
	addTab("sections", SectionListActivity.class, R.string.menu_sections, R.drawable.section);
	addTab("radio", RadioActivity.class, R.string.menu_radio, R.drawable.radio);
	addTab("action", CampaignListActivity.class, R.string.menu_action, R.drawable.campaign);
	addTab("info", InfoActivity.class, R.string.menu_info, R.drawable.info);
	initializeTab();
    }

    @Override
    protected void onRestart() {
	super.onRestart();

	// update tab titles if the language has changed
	if (settingsManager.hasLanguageChanged(lastLanguage)) {
	    updateTabTitles();
	}
    }

    /**
     * Updates the tab titles for the current language.
     */
    private void updateTabTitles() {
	lastLanguage = settingsManager.getArticleLanguage();
	TabWidget tabWidget = tabHost.getTabWidget();

	// go over each of the five tabs
	int tabCount = tabWidget.getChildCount();
	for (int tabIndex = 0; tabIndex < tabCount; tabIndex++) {
	    RelativeLayout relativeLayout = (RelativeLayout) tabWidget.getChildAt(tabIndex);

	    // go over each control within a single tab
	    int childCount = relativeLayout.getChildCount();
	    for (int childIndex = 0; childIndex < childCount; childIndex++) {
		View view = relativeLayout.getChildAt(childIndex);
		if (view instanceof TextView) {
		    // and update the tab titles in the appropriate language
		    TextView textView = (TextView) view;
		    textView.setText(tabTitles[tabIndex]);
		}
	    }
	}
    }

    private void initializeTab() {
	tabHost.setOnTabChangedListener(this);
	tabHost.setVerticalFadingEdgeEnabled(false);
	for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
	    tabHost.getTabWidget().getChildAt(i)
		    .setBackgroundColor(TAB_BACKGROUND_COLOR_UNSELECTED);
	}
    }

    public void onTabChanged(String ignored) {
	for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
	    tabHost.getTabWidget().getChildAt(i)
		    .setBackgroundColor(TAB_BACKGROUND_COLOR_UNSELECTED);
	}

	View tab = tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab());
	tab.setBackgroundColor(TAB_BACKGROUND_COLOR_SELECTED);
    }

    private void addTab(String tag, Class<?> activityClass, int titleId, int iconId) {
	Intent intent = new Intent().setClass(this, activityClass);
	TabSpec tabSpec = tabHost.newTabSpec(tag)
		.setIndicator(res.getString(titleId), res.getDrawable(iconId)).setContent(intent);
	tabHost.addTab(tabSpec);
    }
}